# -*- coding: utf-8 -*-

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import models.trackingInfo as tim
import models.status as sm
import models.user as um
import helpers.maps as maps

"""
Informacje trackingowe mogą oglądać:
  Admin: może oglądać wszystkie ti; ma szybkie linki do edycji i usuwania ti, oraz zleceń, manifestów i użytkowników;
  Driver: może oglądać jedynie swoje, niearchiwalne ti, nie ma mapki
  Manager: może oglądać wszystkie ti; ma szybkie linki do zleceń, manifestów i użytkowników;
  Shipper: może oglądać wszystkie ti; ma szybkie linki do edycji i usuwania ti, oraz zleceń, manifestów i użytkowników;
  User: może oglądać niearchiwalne ti jeśli jest zleceniodawcą, odbiorcą, lub wysyłającym;
"""

def view(role, records, soid ):
  if role == 'driver': 
    response = "<h2>View your tracking informations</h2>"
    response+= '''<table><tr><td><b>Identifier</b></td><td><b>Status</b></td><td><b>Time</b></td><td><b>Position</b></td>
	<td><b>Manifest</b></td><td><b>Modify</b></td></tr>\n''' 
    for record in records:	
      response += '''
	  <tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
	  <td>%s</td><td>%s</td></tr>
        ''' % ( 
		    record.linkTo(text = int(record.ident)),
            sm.StatusModel.show(record.status),
            record.time.strftime('%c'),
			record.positionText if record.positionText else '..',
            str(record.manifest),
			record.linkTo(action='edit') )
    response += '</table>'
    response += '<a href = "/ti/new?order=%d">Add new</a>' % soid
    return response

  response = "<h2>View tracking informations for order no. %d</h2>" % soid

  records = sorted(records, key = lambda x : x.ident )

  record_pos_script = maps.getMapForMultipleCords( [ rec.position for rec in records if rec.position ],
                                                     [ { 'title' : maps.getStringForCords( rec.position ),
                                                         'infobox' : ('''Position: %s <br /> Time: %s'''
                                                                      % (maps.getStringForCords( rec.position ), rec.time.strftime('%c'))) }
                                                       for rec in records if rec.position ]

                                                   )
    
  if record_pos_script:
    record_pos_map = maps.getMapDiv()
  else:
    record_pos_map = ''
        
  response += record_pos_map
  response += record_pos_script

  if role=='user':
    response+= '''<table width="760"><tr><td><b>Identifier</b></td><td><b>Status</b></td><td><b>Time</b></td><td><b>Position</b></td>
	<td><b>Comment</b></td></tr>\n''' 
    for record in records:	
      response += '''
	  <tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
	  <td>%s</td></tr>
        ''' % ( 
		    record.linkTo(text = int(record.ident)),
            sm.StatusModel.show(record.status),
            record.time.strftime('%c'),
			record.positionText if record.positionText else '..',
			record.comment) 
    response += '</table>'
    return response  

  else: 
    response+= '''<table width="760"><tr><td><b>Identifier</b></td><td><b>Status</b></td><td><b>Time</b></td><td><b>Position</b></td>
	<td><b>Manifest</b></td><td><b>Sender</b></td><td><b>Archival</b></td>%s</tr>\n''' % ('' if role == 'manager' else '<td><b>Modify</b></td>')
    for record in records:	
      query = um.UserModel.queryLatest(um.UserModel)
      sender = query.filter('account =', users.User(record.sender)).get()
	
      response += '''
	  <tr %s><td>%s</td><td>%s</td><td>%s</td><td>%s</td>
	  <td>%s</td><td>%s</td><td>%s</td>%s</tr>
        ''' % ( 'class="gt"' if record.archival else '',
		    record.linkTo(text = int(record.ident)),
            sm.StatusModel.show(record.status),
            record.time.strftime('%c'),
			record.positionText if record.positionText else '..',
            record.getManifest().linkTo(text=str(record.manifest)) if record.getManifest() else str(record.manifest),
			sender.linkTo(sender.account.email()) if sender else '',
			record.archival,
			'' if role=='manager' else
			('<td>'+ ('' if record.archival else record.linkTo(action='edit')) +
            ('' if  record.archival else record.linkTo(action='delete')) +'</td>' ) )
    response += '</table>'
    response += '<a href = "/ti/new?order=%d">Add new</a>' % soid
    return response